<?php
namespace app\finance\controller;
use app\common\BaseController;
use app\ota\model\Team as TeamModel;
use app\ota\model\Single as SingleModel;
use app\ota\model\TeamIncome as TeamIncomeModel;
use app\ota\model\TeamOutlay as TeamOutlayModel;

use think\Db;
class Unified extends BaseController{    
    public function __construct(){
        parent::__construct();      
    }
    public function index(){        
        $json = file_get_contents('php://input');                        
        $params = json_decode($json,true);

        //$params = $this->request->param();
        //var_dump($params);exit;
        //type,1：团队，4：单项，5:电子票       
       
        if(empty($params['type'])){
            return $this->callback(0,"缺少分类type");           
        }
       
        if(empty($params['sn'])){
            return $this->callback(0,"缺少订单号sn");          
         }
         if(empty($params['product_name'])){
            return $this->callback(0,"缺少产品名称product_name");           
         }
         if(empty($params['booking_date'])){
            return $this->callback(0,"缺少订单日期booking_date");
         }
         if(empty($params['start_date'])){
            return $this->callback(0,"缺少出发日期start_date");           
         }      
         if(empty($params['operator_id']) || $params['operator_id']==0){
            $params['operator_id'] = 107;
         }           
         if(empty($params['contacts'])){
            return $this->callback(0,"缺少联系人");
         }       
         
         if($params['type']==1){
            return $this->team($params);
         }else{            
            return $this->single($params);
         }
    }

    public function team($params){
      $teamModel = new TeamModel();  
      $isbeing = $teamModel->where('sn',$params['sn'])->find();  
      $team['operator_id'] = $params['operator_id'];      
      $team['type'] = $params['type'];
      $team['category_id'] = $this->category($params['type']);
      $team['title'] = $params['product_name'];
      $team['sn'] = $params['sn'];
      $team['num'] = $params['num'];
      $team['start_date'] = $params['start_date'];
      $team['end_date'] = !empty($params['end_date'])?$params['end_date']:'';
      if($isbeing){
         return $this->callback(0,"订单已存在");         
      }else{
         $team_data = $teamModel->allowField(true)->isUpdate(false)->data($team)->save();         
      }      
      if($team_data){ 
         foreach ($params['guests'] as $k=> $v) {
            //收入            
            $income_data['guest_id']="";
            $income_data['parent_id'] = $teamModel->id;
            $income_data['client_id'] = 126;
            $income_data['num'] = 1;
            $income_data['sell_price'] = $params['sales']['adult_price'];             
            $income_data['kind_id']=43;
            $income_data['salesman_id'] = $params['operator_id'];
            $income_data['type'] = $team['type'];          
            $income_data['guest_id'] = $this->guest($v,$params['operator_id']);
            $teamIncome_data = Db::name('team_income')->insert($income_data);               
         }
         //支出        
         $outlay_data['supplier_id'] = $params['supplier_id'];
         $outlay_data['product_name']=$params['product_name'];
         $outlay_data['kind_id']=43;
         $outlay_data['num']=$params['num'];
         $outlay_data['cost_price']=$params['cost_price']/$params['num'];
         $outlay_data['parent_id'] = $teamModel->id;
         $outlay_data['type'] = $team['type'];           
         $teamOutlay_data = Db::name('team_outlay')->insert($outlay_data);
      }
      return $this->callback(1,"成功");      
    }

    public function single($params){
      $model = new SingleModel(); 
      $params['category_id'] = $this->category($params['type']);          
      $params['supplier_id'] = $params['supplier_id']; 
      $params['operator_id'] = $params['operator_id'];
      
      if(!empty($params['salesman_id'])){
         $params['salesman_id'] = $params['salesman_id'];
      }else{
         $params['salesman_id'] =  $params['operator_id']; 
      }
      if(!empty($params['client_id'])){
         $params['client_id'] = $params['client_id'];
      }else{
         $params['client_id'] = 126; 
      }
      $params['guest_id'] = $this->guest($params['contacts'],$params['operator_id']);      
      $params['sell_price'] = $params['sales']['adult_price'];    
      $params['type'] =$params['type'];
      $isbeing = $model->where('sn',$params['sn'])->find();
      if($isbeing){
         return $this->callback(0,"订单已存在");        
      }else{
         $data = $model->allowField(true)->isUpdate(false)->data($params)->save();
      }            
      return $this->callback(1,"成功");     
    }

    public function guest($params,$operator_id){
      $guest = Db::name('guest')->where("guest_name",$params['name'])->find();            
      if($guest){
         return $guest['id'];            
      }else{      
         Db::name('guest')->insert([
            'guest_name'=>$params['name'],
            'phone'=>$params['phone'],           
            'certificate_type'=>isset($params['certificate_type'])?$params['certificate_type']:0,
            'certificate'=>isset($params['certificate_no'])?$params['certificate_no']:"",
            'operator_id'=>$operator_id
            ]);
         $guestId = Db::name('guest')->getLastInsID();
         return $guestId;          
      }
    }

    public function category($type){
      switch ($type)
      {
      case 1:
        return 42;
        break;  
       case 4:
         return 66;
        break;       
      case 5:
         return 63;
         break;   
      }
   }

    public function callback($code,$msg){
        $message = ["code"=>$code,"msg"=>$msg];
        return json_encode($message,JSON_UNESCAPED_UNICODE);
    }
 }